package cn.jiyun.commons;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

public class ImgUtil {

    public  static String   writefile(HttpSession session, MultipartFile imgfile) throws IOException {
        //获取/images的绝对路径
        String path = session.getServletContext().getRealPath("/images");
        //String path = request.getServletContext().getRealPath("/images");
        //创建文件夹对象
        File dir = new File(path);
        //判断文件夹是否存在，不存在则创建文件夹
        if (!dir.exists()) {
            //创建文件夹对象
            dir.mkdirs();
        }
        //获取上传文件名
        String filename = imgfile.getOriginalFilename();
        //写文件
        imgfile.transferTo(new File(dir, filename));


        return filename;
    }


    public  static void   download(HttpServletResponse response, String filename, XSSFWorkbook wb) throws IOException {
        response.setHeader("Content-Disposition  ", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");
        wb.write(response.getOutputStream());
    }



}
